/**
  ******************************************************************************
  * @file    x509-util.h
  * @author  dragonchern
  * @version V2.2.0
  * @date    09-15-2014
  * @brief   x509 的一些封装
  * @project linux client
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2013 上海今中网络科技有限公司 </center></h2>
  ******************************************************************************
  */

#ifndef __X509__UTIL__H__
#define __X509__UTIL__H__
#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include "basement.h"



typedef enum _x509_cert_type
{
    DER = 0,
    PEM = 1,
}X509_CERT_TYPE_T;

 /*
  * SN: SERIAL NUMBER
  * CN: COMMOM NAME
  * ST: PROVINCE
  * L : CITY
  * O : ORGANIZATION
  * OU: INSTITUTION
  * EA: EMAIL ADDRESS
  */
typedef struct cert_subject
{
    char * serial_num;
    char * common_name;
    char * province;
    char * city;
    char * organization;
    char * institution;
    char * email;
}X509_CERT_SUBJECT_T;

/**
  * @brief   加载der or pem 格式的证书
  * @param   arg1 客户端信息结构体
  * @retval  成功返回0， 失败返回-1， 接收未完成返回1
  */
X509 * x509_init_cert(const char * certpath);


/**
  * @brief   释放证书
  * @param   arg1 用户初始化完成的证书
  * @retval  无
  */
void x509_free_cert(X509 * userCert);

/**
  * @brief   得到证书SN
  * @param   arg1 用户初始化完成的证书
  * @retval  证书的SN号，需要释放
  */
char * x509_cert_sn(X509 * userCert);


/**
  * @brief  获取证书起始日期
  * @param  NULL
  * @retval 证书起始日期
  */
char * x509_cert_start_time(X509 * userCert);


/**
  * @brief  获取证书终止日期
  * @param  NULL
  * @retval 证书终止日期
  */
char * x509_cert_end_time(X509 * userCert);


/**
  * @brief  获取证书使用者CN
  * @param  NULL
  * @retval string for bind, NULL something error happens
  */
char * x509_cert_subject_CN(X509 * userCert);


/**
  * @brief  获取证书使用者的元素信息，用于bindinfo
  * @param
  * @retval -1 failed, 0 success
  */
int x509_cert_subject_ele(X509 * userCert, X509_CERT_SUBJECT_T * cert_suject);


/**
  * @brief  获取证书使用者的字串，用于bindinfo
  * @param
  * @retval NULL or bindinf
  *
  *
  * SN: SERIAL NUMBER
  * CN: COMMOM NAME
  * ST: PROVINCE
  * L : CITY
  * O : ORGANIZATION
  * OU: INSTITUTION
  * EA: EMAIL ADDRESS
  * HW: HARDWARE
  * PT: PLATFORM
  * SP: SMART PAHONE
  * +=========+=====+=====+=====+====+====+=====+=====+========+========+=========+=========+==========+
  * | CONTENT | SN: | CN: | ST: | L: | O: | OU: | EA: | HW PT: | HW SN: | OS VER: | SP No.: | SELF VER |
  *	+=========+=====+=====+=====+====+====+=====+=====+========+========+=========+=========+==========+
  * e.g.: 529024408729859892462242183659:James:北京市:shanghai:org:institution:
  *          aa@163.com:PC:BFEBFBFF000206A7:Windows 7:NULL:V1.0000[:ZDMODEL]
  */
char * x509_cert_bindinfo(X509 * userCert, X509_CERT_SUBJECT_T * cert_subject);


/**
  * @brief  获取证书context
  * @param  NULL
  * @retval
  */
char * x509_cert_context(const char * certPath, int * certSize);


#endif//__X509__UTIL__H__
